package com.huiquan.check_sentence.controller;

import com.huiquan.analysis.constants.AnalysisSentenceConstant;
import com.huiquan.analysis.domain.User;
import com.huiquan.analysis.service.AnalysisSentenceService;
import com.huiquan.check_sentence.service.CheckSentenceService;
import com.huiquan.foundation.constant.TabID;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lichao email:lichao@witspring.com
 * @Description: 外包标注数据校验controller
 * @Date 2017/12/1 10:11
 * @since Ver V
 */
@RequestMapping("/check_sentence")
@Controller
public class CheckSentenceController {

    @Autowired
    private CheckSentenceService checkSentenceService;

    @SuppressWarnings("unchecked")
    @RequestMapping("/list.htm")
    public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) {
        String tabid = req.getParameter("tabid");
        String secondType = req.getParameter("secondType");
        Map<String, Object> pageParam;
        int labelStatus;
        if (StringUtils.isEmpty(secondType)) {
            secondType = AnalysisSentenceConstant.XBS_SOURCE_TYPE_IMAGEOLOGY_INFO;
        }
        if (StringUtils.isNotEmpty(tabid)) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            pageParam.put("secondType", secondType);
            labelStatus = TabID.getAnalysisLabelStatus(tabid);
            pageParam.put("labelStatus", String.valueOf(labelStatus));
            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
            labelStatus = Integer.parseInt(pageParam.get("labelStatus").toString());
        }
        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        return checkSentenceService.list(start, labelStatus, secondType);
    }

    /**
     * 进入词语编辑画面
     *
     * @param req
     * @param resp
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/sentence_pre_edit.htm")
    public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String idStr = req.getParameter("id");

        return checkSentenceService.preEdit(idStr);
    }

    /**
     * 进行词语编辑操作
     *
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping(value = "/sentence_edit.do")
    public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        User user = (User) req.getSession().getAttribute("user");
        String idStr = req.getParameter("id");
        Map<String, Object> map = req.getParameterMap();
        String[] vocabularyStrs = (String[]) map.get("vocabulary");
        String[] characterStrs = (String[]) map.get("character");
        String[] propertyStrs = (String[]) map.get("property");
        String[] position1Strs = (String[]) map.get("position1");
        String[] position2Strs = (String[]) map.get("position2");
        String[] sourceStrs = (String[]) map.get("source");

        String secondType = RequestParamUtil.getParamBySession(req, "secondType");
        ReturnData result = checkSentenceService.edit(String.valueOf(AnalysisSentenceConstant.TYPE_XBS), secondType, idStr, user, vocabularyStrs, characterStrs,
                propertyStrs, position1Strs, position2Strs, req, sourceStrs);
        return result;
    }
}
